package com.zfp.hs.controller;

import com.zfp.core.controller.BaseController;
import com.zfp.core.plugins.page.Page;
import com.zfp.hs.entity.Hospital;
import com.zfp.hs.entity.Order;
import com.zfp.hs.service.HospitalService;
import com.zfp.hs.service.OrderService;
import com.zfp.sys.entity.SysRole;
import com.zfp.sys.entity.SysUser;
import com.zfp.sys.service.SysRoleService;
import com.zfp.sys.service.SysUserService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("hs/order/")
public class OrderController extends BaseController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private HospitalService hospitalService;
	@Autowired
	private SysRoleService sysRoleService;

	@RequestMapping("orderUserList")
	public String orderUserList(HttpSession session, Model model, SysUser sysUser, Page<SysUser> page, String isfront){
		SysUser sysUserSession = (SysUser) session.getAttribute("sessionUser");
		List<SysRole> roles = sysRoleService.list("SysRole.findUserRoles",sysUserSession.getUserId());
		for(SysRole role : roles){
			if(role.getRoleId() == 4){
				sysUser.setUserId(sysUserSession.getUserId());
				isfront = "1";
			}
		}
		page = sysUserService.pageList("Order.orderUserList",page,sysUser);
		model.addAttribute("page",page);
		model.addAttribute("sysUser",sysUser);
		model.addAttribute("isfront",isfront);
		return "hs/orderUserList";
	}
	@RequestMapping("orderDoctorList")
	public String orderDoctorList(HttpSession session,Model model, SysUser sysUser, Page<SysUser> page,String isfront){
		page = sysUserService.pageList("Order.orderDoctorList",page,sysUser);
		model.addAttribute("page",page);
		model.addAttribute("sysUser",sysUser);
		model.addAttribute("isfront",isfront);
		return "hs/orderDoctorList";
	}

	@RequestMapping("orderList")
	public String orderList(Model model, Order order, Page<Order> page){
		page = orderService.pageList("Order.orderList",page,order);
		model.addAttribute("page",page);
		model.addAttribute("order",order);
		return "hs/orderList";
	}
	@RequestMapping("turnSave")
	public String turn(Order order,Model model,String isfront){
		if(order.getId() != null){
			Order orderupdate=orderService.list("Order.orderList", order).get(0);
			model.addAttribute("orderupdate",orderupdate);
            model.addAttribute("isupdate","1");
		}
		SysUser sysUser = new SysUser();
		List<Hospital> hospital = hospitalService.list("Hospital.getHospitalByBelongedid",sysUser);
		model.addAttribute("hospital",hospital);
		model.addAttribute("order",order);
		model.addAttribute("isfront",isfront);
		return "hs/orderSavePage";
	}

    @RequestMapping("save")
    public void save(PrintWriter out, Order order){
        //System.out.println(sysUser.getUserId());
        Boolean b = orderService.save(order);
        //System.out.println(b);
        out.print(b);
        out.close();
    }

    @RequestMapping("delete")
    public void delete(PrintWriter out,Order order){

        Boolean b = orderService.deleteById(order);
        out.print(b);
        out.close();
    }

	@RequestMapping("getDcByBelongedid")
	@ResponseBody
	public String getDcByBelongedid(Order order){
		List<Order> doctors = orderService.list("Order.getDcByBelongedid",order);
		JSONArray listArray=JSONArray.fromObject(doctors);
		return listArray.toString();
	}

    @RequestMapping("getDateByDoctor")
    @ResponseBody
    public String getDateByDoctor(Order order){
        List<Order> visitdates = orderService.list("Order.getDateByDoctor",order);
        JSONArray listArray=JSONArray.fromObject(visitdates);
        return listArray.toString();
    }

    @RequestMapping("getTimesByDate")
    @ResponseBody
    public String getTimesByDate(Order order){
        List<Order> visitdates = orderService.list("Order.getTimesByDate",order);
        JSONArray listArray=JSONArray.fromObject(visitdates);
        return listArray.toString();
    }

    @RequestMapping("feedback")
    public String feedback(Model model,Order order){
        if(order.getId() != null){
            Order orderupdate=orderService.list("Order.orderList", order).get(0);
            model.addAttribute("orderupdate",orderupdate);
        }
        model.addAttribute("order",order);
        return "hs/orderFeedback";
    }
    @RequestMapping("savefeedback")
    public void savefeedback(PrintWriter out, Order order){
        //System.out.println(sysUser.getUserId());
        Boolean b = orderService.savefeedback(order);
        //System.out.println(b);
        out.print(b);
        out.close();
    }

	@RequestMapping("preOrder")
	public String preOrder(Model model,Order order){
        orderService.preOrder(order,model);

		return "hs/preOrder";
	}
}